﻿using Prism.Events;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UI.Application.Share.Event;
using UI.Application.Share.Initialize;
using UI.Application.Share.Model;

namespace UI.Application.Login.ViewModels
{
    //这里就internal可以不需要改
    internal class LoginViewModel:ReactiveObject
    {
        private IEventAggregator EventAggregator { get; }
        public string Title { get; }
        public string Version => System.Windows.Application.ResourceAssembly.GetName().Version.ToString();
        public CurrentUser CurrentUser { get; set; } =new CurrentUser() { UserName = "admin", Password = "12345678" };
        public ICommand LoginCommand { get; }
        public LoginViewModel(IEventAggregator eventAggregator)
        {
            EventAggregator = eventAggregator;
            LoginCommand = ReactiveCommand.Create(Login);
        }

        private void Login()
        {
            if(string.IsNullOrEmpty(CurrentUser.UserName)||string.IsNullOrEmpty(CurrentUser.Password))
            {
                return;
            }
            if(CurrentUser.UserName=="admin" && CurrentUser.Password=="12345678")
            {
                //登入成功后加载Initialize项目
                //用prism的事件聚合器
                //触发的时候要传一些参数
                EventAggregator.GetEvent<LoginSuccessEvent>().Publish(CurrentUser);//通知用户登入成功。在shell里面订阅这个事件
            }
        }
    }
}
